if(localStorage.getItem("userInfos")){
	hui.open('homepage.html',{},true);
} 

//var thatQuit = true;
//判断是否点击退出标识
console.log("thatQuit刚进页面"+sessionStorage.getItem("thatQuit"))
if(sessionStorage.getItem("thatQuit")==1 || sessionStorage.getItem("thatQuit") == null){
	
	sessionStorage.setItem("thatQuit",1);
}

function getnologintoken(){
	hui.ajax({
	    type : 'GET',
	    url : globleobj['domain']+'/doctor/getnologintoken',
	    data : {},
	    beforeSend : function(){hui.loading();},
	    complete   : function(){hui.closeLoading();}, 
	    success : function(msg){
	    	var data = JSON.parse(msg);
	    	localStorage.setItem("noLoginToken",data.noLoginToken);	
	    	//alert(localStorage.getItem('noLoginToken'));  
	    },
	    error : function(e){
	        console.log(JSON.stringify(e));
	        hui.iconToast('读取消息失败', 'warn');
	    } 
	}); 
}
var song = "song"
//开始倒计时
var countdown = 60; 
function settime(obj) { 
    if (countdown == 0) { 
        obj.removeAttribute("disabled");
        addClass(obj,'thatcode')
        obj.value="获取验证码";
        countdown = 60;
        return;
    } else { 
        obj.setAttribute("disabled", true); 
        removeClass(obj,'thatcode')
        obj.value="重新发送(" + countdown + ")";
        countdown--;
    } 
	setTimeout(function() {
	    settime(obj) }
	,1000)
} 
//登陆
function reglogin(thatData){
	
	hui.ajax({
	    type : 'GET',
	    url : globleobj['domain']+'/wuser/loggin/'+thatData.waccount+'/'+thatData.wpassword,
	    data : '',
	    beforeSend : function(){hui.loading();},
	    complete   : function(){hui.closeLoading();},
	    success : function(msg){
	    	var data = JSON.parse(msg);
	    	console.log('登陆:'+msg)
	    	if(data.rc != 0){
	    		hui.toast(data.showmsg); 
	    		return false;
	    	}
	    	if(data.body.length < 1){
	    		hui.toast('账号不正确'); 
	    		return false;
	    	}
	    	localStorage.setItem("userInfos",JSON.stringify(data.body[0]));	
	    	console.log("login"+localStorage.getItem('userInfos'));
//	    	setTimeout(function(){	
//	    		var thatQuit = sessionStorage.getItem("thatQuit");	
//	    		console.log('登陆的：'+thatQuit);
//	    		if(thatQuit != 1){
//	    			hui.getViewById('homepage.html').reload();
//	    		}
//				sessionStorage.setItem("thatQuit",2);
//	    	},300)
	    	hui.open('homepage.html',{},true); 
	    },
	    error : function(e){
	    	
			alert(2)
	        console.log(JSON.stringify(e));
	        hui.iconToast('读取消息失败', 'warn');
	    } 
	}); 
	
}

hui.ready(function(){ 
	hui("#userPhone").val('');
	hui("#yanzheng").val('');
	//getnologintoken();
	//删除输入框内容
	hui.formInit();
	//注册
	hui('#login_register').click(function(){
	    hui.open('register.html?name='+song,{},true,{name:'song'})
	});
	//登陆
	hui('#login_submit').click(function(){
		var res = huiFormCheck('#form1');
		if(res){	
			var thatData = {
				'waccount':hui("#userPhone").val(),
				'wpassword':hui("#yanzheng").val()
			}
			reglogin(thatData);
	    } 
		
	    
	});
	hui("#getCode").click(function() {
		var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
		if(hui("#userPhone").val().match(reg)){
			var noLoginToken  = localStorage.getItem('noLoginToken');
			console.log(noLoginToken)
			var salt = noLoginToken.charAt(2)+noLoginToken.charAt(4)+noLoginToken.charAt(6)+noLoginToken.charAt(8);
			console.log(salt)
			var ts = Math.round(new Date().getTime()/1000).toString();
			var str = 'noLoginToken='+noLoginToken+'&phone='+hui("#userPhone").val()+'&timestamp='+ts;
			console.log(str)
			var signature = hex_md5(str+salt);
			var thatData = {
				'phone':hui("#userPhone").val(),
				'noLoginToken':noLoginToken,
				'timestamp':ts,
				'signature':signature,
				
			}
			console.log(thatData)
			hui.ajax({
			    type : 'POST',
			    url : globleobj['domain']+'/doctor/sendphonecode',
			    data : thatData,
			    beforeSend : function(){hui.loading();},
			    complete   : function(){hui.closeLoading();},
			    success : function(msg){
			    	var data = JSON.parse(msg);
			    	console.log(msg)
			    	if(data.rc != 0){
			    		hui.toast(data.msg);  
			    		return false;
			    	}
			    	var thisCode =document.getElementById("thiscode");
				    settime(thisCode);
			    },
			    error : function(e){
			        console.log(JSON.stringify(e));
			        hui.iconToast('读取消息失败', 'warn');
			    } 
			}); 
			//sendphonecode(thatData)
			
		}else{
			hui.toast('请输入正确手机号码');
		}
	})
	//忘记密码
	hui('#login_forgetPw').click(function(){
	    hui.open('fogetPassword.html')
	});
    hui.plusReady(function(){
    	console.log('plusReady')
    });
});